<!DOCTYPE html>
<html>
<head>
    <title>添加收费项目--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../../Css/style.css"/>
    <script type="text/javascript" src="../../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../../Js/ckform.js"></script>
    <script type="text/javascript" src="../../Js/common.js"></script>
    <script type="text/javascript" src="../../Js/ckeditor/ckeditor.js"></script>


    <style type="text/css">
        body {
            padding-bottom: 40px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }

    </style>
    <script type="text/javascript">
        let flag = false;

        $(function () {
            $('#backid').click(function () {
                window.location.href = "charge.html";
            });

            loadData();
        });

        String.prototype.GetValue = function (para) {
            let reg = new RegExp("(^|&)" + para + "=([^&]*)(&|$)");
            let r = this.substr(this.indexOf("\?") + 1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        }

        function loadData() {
            let url = document.location.toString();
            let chapId = url.GetValue("chapId");
            if (chapId == undefined || chapId === null || chapId == 0) {
                return;
            }
            let readonly = url.GetValue("readonly");
            if (readonly == "true") {
                $("#saveOrAddBtn").prop("disabled", true);
            }
            console.log("readonly = " + readonly)
            console.log(readonly == "true")

            $.ajax({
                type: "GET",
                url: `/charge-project/${chapId}`,
                dataType: "json",
                success: function (res) {
                    if (res.code == 200) {
                        let obj = res.obj;
                        $("#chapId").html(obj.chapId);
                        $("input[name='chapName']").val(obj.chapName);
                        $("input[name='chapMoney']").val(obj.chapMoney);
                        if (readonly == "true") {
                            $("input[name='chapName']").prop("disabled", true);
                            $("input[name='chapMoney']").prop("disabled", true);
                        }
                    }
                }
            })
        }

        // 保存
        function save() {
            // 不使用表单方式提交请求
            event.preventDefault();
            // 获取请求参数
            let chapId = $("#chapId").html();
            if (chapId == "自动生成" || chapId == "") {
                chapId = null;
            }
            let chapName = $("input[name='chapName']").val();
            let chapMoney = $("input[name='chapMoney']").val();

            if (isNaN(chapMoney) == true) {
                alert("收费金额只能为数字类型!");
                return;
            }

            checkNameDiff();

            setTimeout(() => {
                if (flag == false) {
                    return;
                }
                $.ajax({
                    type: "POST",
                    url: "/charge-project/save",
                    dataType: "json",
                    data: {
                        chapId, chapName, chapMoney
                    },
                    success: function (res) {
                        alert(res.msg);
                        if (res.code === 200) {
                            // 跳转至查询页
                            $(location).attr("href", "charge.html");
                        }
                    }
                });
            }, 1000);
        }

        // 检查收费项目名称是否重复
        function checkNameDiff() {
            flag = false;
            let chapName = $("input[name='chapName']").val();
            $.ajax({
                type: "GET",
                url: "/charge-project/check",
                dataType: "json",
                data: {
                    chapName
                },
                success: function (res) {
                    if (res.code === 200) {
                        flag = true;
                    } else {
                        alert("当前收费项目名称已存在");
                    }
                }
            });
        }
    </script>
</head>
<body>
<form action="charge.html" method="post" class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">编号</td>
            <td id="chapId">自动生成</td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">收费项目名称</td>
            <td><input type="text" onblur="checkNameDiff()" name="chapName" value="心电图图检"/></td>
        </tr>

        <tr>
            <td width="10%" class="tableleft">收费金额</td>
            <td><input type="text" name="chapMoney" value="600"/></td>
        </tr>
        <tr>
            <td colspan="2">
                <center>
                    <button id="saveOrAddBtn" onclick="save()" class="btn btn-primary" type="button">保存</button>
                    &nbsp;&nbsp;<button
                        type="button" class="btn btn-success" name="backid" id="backid">返回列表
                </button>
                </center>
            </td>
        </tr>
    </table>
</form>
</body>
</html>